package m202406.d08;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/8 22:37
 */
public class Q14 {
    public String longestCommonPrefix(String[] strs) {
        String min = strs[0];
        for (String str : strs) {
            if (str.length() < min.length()) {
                min = str;
            }
        }
        StringBuilder sb = new StringBuilder(min);
        for (String str : strs) {
            int length = Math.min(sb.length(),str.length());
            for (int i = 0; i < length; i++) {
                if (sb.charAt(i) != str.charAt(i)) {
                    sb = sb.delete(i, sb.length());
                    break;
                }
            }
        }
        return sb.toString();
    }
}
